<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
      
      $showclassl=$this->getlist("","classl","","","","classl_order","asc");
      foreach($showclassl as $key =>$value){
         $goods=M("goods")
               ->limit("0,12")
               ->where(array("classl_id"=>$value['classl_id']))
               ->select();
          foreach($goods as &$vaa){
               $string=getalbumface($vaa['go_pics']);


               $vaa['go_pics']=$string?$string:'';
               
          }
         $good_array=array(
                "go_pics"=>__ROOT__."/Public/home/images/no_img.jpg",
                "go_value"=>"0.00",
                "go_name"=>"暂无商品"
                );
         $showclassl[$key]['goods']=array_pad($goods,12,$good_array);
         $showclassl[$key]['m_class']=M("classm")
                                     ->field("classm_id","name")
                                     ->where(array("classl_id"=>$value['classl_id']))
                                     ->select();
      }
      $this->getclass();//左侧导航菜单
      $this->article(12);//商城公告
      $this->assign("show_classl",$showclassl);
      $this->assign("head_tittle","霞客商城");
      $this->display();
    }
    /**
     * [getclass 获取分类（首页侧边导航块）的方法]
     * @return [type] [description]
     */
    function getclass(){
        $classl=$this->getlist("","classl","","","","classl_order","asc");
        foreach ($classl as $key => $value) {
          $m=$this->getlist("D","classm","",array('classl_id'=>$value['classl_id']),"","classm_order","asc","",true);
          $s=array("classs_id"=>0,"classs_name"=>"三级分类");
          $replace_array=array(
                "classm_id"=>0,
                "classm_name"=>"二级分类",
                "classs"=>array_pad(array(array("classs_id"=>0,"classs_name"=>"三级分类")),8,$s)
            );
          $classl[$key]['classm']=array_pad($m,8,$replace_array);
          //$classl[$key]['classm']=$m;
        }
        $this->assign("classl",$classl);
    }
    /**
     * [article 获取文章的方法]
     * @param  integer $limit [description]
     * @return [type]         [description]
     */
    function article($limit=10){
        $arlist=M('Article')
               ->field(array('ar_id','ar_tittle'))
               ->limit($limit) 
               ->select();
        $this->assign('arlist',$arlist);
    }
    /**
     * [goodsList 列表页]
     * @return [type] [description]
     */
    function goodsList(){
       $classl_id=I('lid');
       $classm_id=I('mid');
       $classs_id=I('sid');
       //根据三层分类id获取classl_id
       if(!empty($classs_id)){
          $classl_id=M('Classs')
                    ->where(array('classs_id'=>$classs_id))
                    ->getField('classl_id');
          //第三层分类id
          $map=array('classs_id'=>$classs_id);
       }else{
          if(!empty($classm_id)){
               $classl_id=M('Classm')
                    ->where(array('classm_id'=>$classm_id))
                    ->getField('classl_id');
                $map=array('classm_id'=>$classm_id);
          }else{
               if(empty($classs_id)) $classs_id=10;
               $map=array('classl_id'=>$classl_id);
          }
       }
       $classm=M('classm')
              ->field(array('classm_id','classm_name'))
              ->where(array('classl_id'=>$classl_id))
              ->select();
        $mclassm=M('classs');
        foreach ($classm as  &$value) {
            $value['classs']=$mclassm
                            ->field(array('classs_id','classs_name'))
                            ->where(array('classm_id'=>$value['classm_id']))
                            ->select();
        }
        //猜你喜欢
        $this->youlike();
        //商品列
        $mgoods=M('goods');
        $count=$mgoods->where($map)->count();
        $limit=R('Public/fenpage',array($count,'20','page'));
        $gList=$mgoods
              ->where($map)
              ->field(array('go_name','go_value','go_id','go_pics'))
              ->limit($limit)
              ->select();
        foreach ($gList as &$value) {
            $value['go_pics']=getalbumface($value['go_pics']);
        }
              

       $this->getclass();//左侧导航菜单
        $this->assign('glist',$gList);
       $this->assign("head_tittle","霞客商城");
       $this->assign('classm',$classm);
       $this->display();
    }
    /**
     * [youlike 获取猜你喜欢的数据]
     * @param  integer $limit [数据限制条数]
     * @return [type]         [description]
     */
    function youlike($limit=5){
        $youlike=M('goods')
                ->field(array('go_name','go_value','go_id','go_pics'))
                ->limit($limit)
                ->select();
        foreach ($youlike as &$value) {
             $value['go_pics']=getalbumface($value['go_pics']);
        }
                
        $this->assign('youlike',$youlike);
    }

    /**
     * [detail 商品展示详情]
     * @return [type] [description]
     */
    function detail(){
       $id=I("request.id");
       $data=D("GoodsView")->where(array("go_id"=>$id))->find();
       $pics=$data['go_pics'];
       $array=explode(',',$pics);
       foreach ($array as $value) {
            if($value){
                   $pic[]=array(
                        'bigimg'=>__ROOT__.$value,
                        'smallimg'=>__ROOT__.str_replace(basename($value),'ss'.basename($value),$value)
                    ); 

            }   
       }
       //猜你喜欢
       $this->youlike();
       $this->getclass();//左侧导航菜单
       $this->assign('go_pics',$pic);
       $this->assign("list",$data);
       $this->assign("head_tittle",$data['go_name']);
       $this->display();
    }
    

    /**
     * [shop 购物车页面]
     * @return [type] [description]
     */
    function shop(){
       session_start();
       $data=array();
       if(count($_SESSION['shop'])>0){
           $id_array=R("Public/get_array",array($_SESSION['shop'],"id"));
           $data=M("goods")->where(array("go_id"=>array("in",$id_array)))->select();
           foreach ($data as $key => $value) {
                $data[$key]['shop_num']=$_SESSION['shop'][$value['go_id']]['number'];
           }  
       }
       $this->assign("shop",$data);
       $this->display();
    }
    //成功加入购物车
    function shop_success(){
      $id=$_REQUEST['id'];
       session_start();
       if(!$_SESSION[$id]){
          $_SESSION['shop'][$id]=array("id"=>$id,"number"=>1);
       }
       $this->display();
    }
    //商品订单
    function goods_order(){
       R("Public/check_login");
       $uid=is_login();
       
       //购物车商品信息
       $return=getShop();
       $order_good=$return['order_good'];
       $total=$return['total'];

       
       $pageconfig=array(
          "tittle"=>"--订单提交",
       );
       //会员信息
       $user=getUserInfo();

       //支付方式
       $payway=R('Public/getPayWay');
       //配送方式
       $sendway=R('Public/getSendWay');

       $pageconfig['total_value']=$total;
       $this->assign("order_good",$order_good);
       $this->assign('pageconfig',$pageconfig);
       $this->assign('address',json_decode($user['ui_address'],true));
       $this->assign('payway',$payway);
       $this->assign('sendway',$sendway);
       $this->display();
    }
    //订单提交
    function order_submit(){
       if($_POST['order_submit']){
          $now=mktime();
          $uid=is_login();
          $order=make_order();//生成订单号
          $morder=D("Oorder");
          

          //购物车商品信息
          $return=getShop();
          $order_good=$return['order_good'];
          $total=$return['total'];

          //处理订单信息
          $data=$morder->create();
          if(!$data){
              $this->error($morder->geterror());
          }

          $address=getJsonAddress($_POST['address']);
          $data['od_createtime']=$now;
          $data['user_id']=$uid;
          $data['od_no']=$order;
          $data['od_value']=$total;
          $data['od_address']=json_encode($address);
          $rorder=$morder->add($data);

          //处理与订单相关的商品存储
          
          foreach ($order_good as $value) {
              $ginfo[]=array(
                      'go_id'=>$value['go_info']['go_id'],
                      'go_value'=>$value['go_info']['go_value'],
                      'go_number'=>$value['number'],
                );
          }
          $ogoods=array(
                'od_id'=>$rorder,
                'user_id'=>$uid,
                'odg_json'=>json_encode($ginfo),
                'odg_total'=>$total
          );
          $mogoods=D("OrderGoods");
          $rogoods=$mogoods->add($ogoods);

          
          if($rorder&&$rogoods){
              if($data['payway_id']==1){
                //在线支付
                R('Public/alipay',array());
                
              }elseif($data['pay_way']==2){
                //货到付款
                $this->success("下单成功");
              }
          } 
       }

       
    }

    function get_array($array,$obj){
    $return_array=array();
        foreach ($array as $key => $value) {
          array_push($return_array,$value[$obj]);
        }
        return $return_array;
  }
    //登录
    function login(){
       $pageconfig=array(
          "tittle"=>"欢迎登录",
       );
       $this->display();
    }

     //注册
    function reg(){
      if($_POST){
         $user=D("user");
         $data=$user->create($_POST);

         $dis=substr(md5(substr($_POST['userpsd'],0,2)),0,2);
         $psd=md5($_POST['userpsd'].$dis);

         $data['user_password']=$psd;
         $data['user_createtime']=mktime();
         $user->add($data); 
         if($user){
              $this->success('注册成功，请登录','login');
              die();
         }
      } 
       $pageconfig=array(
          "tittle"=>"--注册用户",
       );
       $this->assign('pageconfig',$pageconfig);
       $this->display();
    
 }




public function addlist($m,$table,$data){
      if($m=="") $m="M";
      $m($table)->add($data);
    }
    public function getlist($m,$table,$field,$where,$num,$orderobj,$adsc,$select,$relation){
      if($m=="") $m="M";
      if($orderobj=="" || $adsc=="") $order=""; 
      else $order= $orderobj." ".$adsc;
      if($select=="") $select="select";
      if( $num!=="") {
        $count=$m($table)->where($where)->count();
        $Page= new \Think\Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show= $Page->show();// 分页显示输出
        $limit= $Page->firstRow." , ".$Page->listRows;
        $this->assign('page',$show);// 赋值分页输出
      }else {$limit= "";}
      if($relation==""){ 
        $data= $m($table)->field($field)->where($where)->order($order)->limit($limit)->$select();
        return $data;
      }else{
       $data= $m($table)->field($field)->where($where)->order($order)->relation($relation)->limit($limit)->$select();
        return $data;
      }
    }

  public function shiyan(){
      //$image = new \Think\Image(); 
      //$image->open('./attachment/goodpic/2015/12/27/567fa51fc4952.jpg');
      // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
      //$image->thumb(150, 150)->save('./attachment/goodpic/2015/12/27/thumb.jpg');
      // 1. 初始化
$user = "admin";
$pass = "admin100";
$curlPost = "user=$user&pass=$pass";
$url=$_SERVER['SERVER_NAME'].__ROOT__."/alipay/alipayapi.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);
curl_close($ch);

  }
}